package com.situ.system.department.servlet;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.situ.system.department.model.DepartmentModel;
import com.situ.system.department.service.DepartmentService;
import com.situ.system.util.FmtRequest;
import com.situ.system.util.Pager;

import sun.management.counter.Variability;

/**
 * Servlet implementation class DepartServlet
 */
public class DepartServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	DepartmentService service = new DepartmentService();

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public DepartServlet() {
		super();

	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.getWriter().append("Served at: ").append(request.getContextPath());
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */

	// 对于response来说，是属于写入的，也就是由Java向ajax写入
	// 对于request来说，是属于读取的，主要是将前端的值，获取出来对数据库进行操作
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		String action = request.getParameter("action");
		Object res = "";
		switch (action) {
		case "list":
			res = list(request);
			break;
		case "page": // 分页/查询使用
			res = page(request);
			break;
		case "del": // 删除操作
			res = del(request);
			break;
		case "add": // 添加操作
			res = add(request);
			break;
		case "get": // 获取一个Model的值，修改前的回显
			res = get(request);
			break;
		case "udp":
			res = udp(request);
			break;
		default:
			break;
		}
		FmtRequest.write(response.getWriter(), res);
	}

	private List<DepartmentModel> list(HttpServletRequest req) {
		String code = req.getParameter("code");
		String name = req.getParameter("name");
		DepartmentModel model = new DepartmentModel();
		model.setId(code);
		model.setName(name);
		return service.selectList(model);
	}

	private Map<String, Object> page(HttpServletRequest request) {
		DepartmentModel model = new DepartmentModel();
		String id = request.getParameter("bianhao");
		String name = request.getParameter("name");
		model.setId(id);
		model.setName(name);
		int pageIndex = Integer.parseInt(request.getParameter("pageIndex"));
		int pageLimit = Integer.parseInt(request.getParameter("pageLimit"));
		model.setPageIndex(pageIndex);
		model.setPageLimit(pageLimit);
		model.setPageOn(true);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("data", service.selectList(model));
		map.put("count", service.selectCount(model));
		return map;
	}

	private String del(HttpServletRequest req) {
		String code = req.getParameter("id");
		DepartmentModel model = new DepartmentModel();
		model.setId(code);
		return service.delete(model);
	}

	private String add(HttpServletRequest req) {
		String id = req.getParameter("code");
		String name = req.getParameter("name");
		String tel = req.getParameter("tel");
		DepartmentModel model = new DepartmentModel();
		model.setId(id);
		model.setName(name);
		model.setTel(tel);
		return service.insert(model);
	}

	private DepartmentModel get(HttpServletRequest req) {
		String id = req.getParameter("code");
		DepartmentModel model = new DepartmentModel();
		model.setId(id);
		return service.selectModel(model);
	}

	private String udp(HttpServletRequest req) {
		String id = req.getParameter("code");
		String name = req.getParameter("name");
		String tel = req.getParameter("tel");
		System.out.println(name);
		DepartmentModel model = new DepartmentModel(id, name, tel);
		return service.update(model);
	}
}
